package fr.LesLoutresSauvages.laloutresportive.util;

import java.util.ArrayList;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

public class Live implements JsonConvertible
{

  private int id;
  private String nom;
  private String created;
  private String equipe1;
  private String equipe2;
  private int scoreEquipe1;
  private int scoreEquipe2;
  private String nomCommentateur;
  private String latitude;
  private String longitude;
  private String shortDescription;
  private String longDescription;
  private String debut;
  
  private Sport s;
  private Competition c;
  private Departement d;
  public ArrayList<Evenement> arrliEvenements;

  
  
  
  
  
  public Live(
              int id, 
              String nom, 
              String created, 
              String equipe1, 
              String equipe2, 
              int scoreEquipe1, 
              int scoreEquipe2, 
              String nomCommentateur,      
              String latitude, 
              String longitude, 
              String shortDescription, 
              String longDescription, 
              String debut,
              Sport s, 
              Competition c,
              Departement d)
  {
    super();
    this.id = id;
    this.nom = nom;
    this.created = created;
    this.equipe1 = equipe1;
    this.equipe2 = equipe2;
    this.scoreEquipe1 = scoreEquipe1;
    this.scoreEquipe2 = scoreEquipe2;
    this.nomCommentateur = nomCommentateur;
    this.latitude = latitude;
    this.longitude = longitude;
    this.shortDescription = shortDescription;
    this.longDescription = longDescription;
    this.debut = debut;
    this.s = s;
    this.c = c;
    this.d = d;
    this.arrliEvenements = new ArrayList<Evenement>();
  }

  public Live()
  {
    super();
    this.id = 0;
    this.nom = "";
    this.created = "";
    this.equipe1 = "";
    this.equipe2 = "";
    this.scoreEquipe1 = 0;
    this.scoreEquipe2 = 0;
    this.nomCommentateur = "";
    this.latitude = "";
    this.longitude = "";
    this.shortDescription = "";
    this.longDescription = "";
    this.debut = "";
    this.s = new Sport();
    this.c = new Competition();
    this.d = new Departement();
    this.arrliEvenements = new ArrayList<Evenement>();
  }

  // TODO : AlexX : Supprimer tous les Parcel et les Parcelable pour enregistrer le live en global

  
  
  
  
  
  
  
  public void setAll(
      int id, 
      String nom, 
      String created, 
      String equipe1, 
      String equipe2, 
      int scoreEquipe1, 
      int scoreEquipe2, 
      String nomCommentateur,      
      String latitude, 
      String longitude, 
      String shortDescription, 
      String longDescription, 
      String debut)
  {
    this.id = id;
    this.nom = nom;
    this.created = created;
    this.equipe1 = equipe1;
    this.equipe2 = equipe2;
    this.scoreEquipe1 = scoreEquipe1;
    this.scoreEquipe2 = scoreEquipe2;
    this.nomCommentateur = nomCommentateur;
    this.latitude = latitude;
    this.longitude = longitude;
    this.shortDescription = shortDescription;
    this.longDescription = longDescription;
    this.debut = debut;
  }

  public int getId()
  {
    return id;
  }

  public void setId(int id)
  {
    this.id = id;
  }

  public String getNom()
  {
    return nom;
  }

  public void setNom(String nom)
  {
    this.nom = nom;
  }

  public String getCreated()
  {
    return created;
  }

  public void setCreated(String created)
  {
    this.created = created;
  }

  public String getEquipe1()
  {
    return equipe1;
  }

  public void setEquipe1(String equipe1)
  {
    this.equipe1 = equipe1;
  }

  public String getEquipe2()
  {
    return equipe2;
  }

  public void setEquipe2(String equipe2)
  {
    this.equipe2 = equipe2;
  }

  public int getScoreEquipe1()
  {
    return scoreEquipe1;
  }

  public void setScoreEquipe1(int scoreEquipe1)
  {
    this.scoreEquipe1 = scoreEquipe1;
  }

  public int getScoreEquipe2()
  {
    return scoreEquipe2;
  }

  public void setScoreEquipe2(int scoreEquipe2)
  {
    this.scoreEquipe2 = scoreEquipe2;
  }

  public String getNomCommentateur()
  {
    return nomCommentateur;
  }

  public void setNomCommentateur(String nomCommentateur)
  {
    this.nomCommentateur = nomCommentateur;
  }

  public String getLatitude()
  {
    return latitude;
  }

  public void setLatitude(String latitude)
  {
    this.latitude = latitude;
  }

  public String getLongitude()
  {
    return longitude;
  }

  public void setLongitude(String longitude)
  {
    this.longitude = longitude;
  }

  public String getShortDescription()
  {
    return shortDescription;
  }

  public void setShortDescription(String shortDescription)
  {
    this.shortDescription = shortDescription;
  }

  public String getLongDescription()
  {
    return longDescription;
  }

  public void setLongDescription(String longDescription)
  {
    this.longDescription = longDescription;
  }

  public String getDebut()
  {
    return debut;
  }

  public void setDebut(String debut)
  {
    this.debut = debut;
  }

  public Sport getSport()
  {
    return s;
  }

  public void setSport(Sport s)
  {
    this.s = s;
  }

  public Competition getCompetition()
  {
    return c;
  }

  public void setCompetition(Competition c)
  {
    this.c = c;
  }

  public Departement getDepartement()
  {
    return d;
  }

  public void setDepartement(Departement d)
  {
    this.d = d;
  }
  
  
  
  
  
  
  
  
  

  @Override
  public String toString()
  {
    return "Live [id=" + id + ", nom=" + nom + ", created=" + created
        + ", equipe1=" + equipe1 + ", equipe2=" + equipe2 + ", scoreEquipe1="
        + scoreEquipe1 + ", scoreEquipe2=" + scoreEquipe2
        + ", nomCommentateur=" + nomCommentateur + ", latitude=" + latitude
        + ", longitude=" + longitude + ", shortDescription=" + shortDescription
        + ", longDescription=" + longDescription + ", debut=" + debut + ", s="
        + s.toString() + ", c=" + c.toString() + ", d=" + d.toString() + ", arrliEvenements=" + arrliEvenements
        + "]";
  }

  
  
  
  

  @Override
  public String toJSON()
  {
    JSONObject jObj = new JSONObject();
    try
    {
      jObj.put("id", this.getId());
      jObj.put("nom", this.getNom());
      jObj.put("commentateur", this.getNomCommentateur());
      jObj.put("equipe1", this.getEquipe1());
      jObj.put("equipe2", this.getEquipe2());
      jObj.put("scoreEquipe1", this.getScoreEquipe1());
      jObj.put("scoreEquipe2", this.getScoreEquipe2());
      jObj.put("competition", this.getCompetition().toJSON()); // Genere des '\' avant les guillemets et genere des guillemets autour du JSON incruste. Le fromJSON() le decrypte correctement, a tester avec le serveur du concour.
      jObj.put("departement", this.getDepartement().toJSON()); // Genere des '\' avant les guillemets et genere des guillemets autour du JSON incruste. Le fromJSON() le decrypte correctement, a tester avec le serveur du concour.
      jObj.put("latitude", this.getLatitude());
      jObj.put("longitude", this.getLongitude());
      jObj.put("shortDescription", this.getShortDescription());
      jObj.put("longDescription", this.getLongDescription());
      jObj.put("debut", this.getDebut());
      jObj.put("sport", this.getSport().toJSON()); // Genere des '\' avant les guillemets et genere des guillemets autour du JSON incruste. Le fromJSON() le decrypte correctement, a tester avec le serveur du concour.

      JSONArray jArr = new JSONArray();
      for ( int i=0 ; i<this.arrliEvenements.size(); i++ )
      {
        JSONObject jObjEvenement = new JSONObject();
        
        jObjEvenement.put("id", this.arrliEvenements.get(i).getId());
        jObjEvenement.put("commentaire", this.arrliEvenements.get(i).getCommentaire());
        
        jArr.put(jObjEvenement);
      }

      jObj.put("evenements", jArr);
    }
    catch (JSONException e)
    {
      e.printStackTrace();
    }
    
    return ( jObj.toString() );
  }

  @Override
  public void fromJSON(String str)
  {
    try
    {
      JSONObject jObj = new JSONObject(str);
      JSONArray jArr = null;

      this.setId(jObj.getInt("id"));
      this.setNom(jObj.getString("nom"));
      this.setNomCommentateur(jObj.getString("commentateur"));
      this.setEquipe1(jObj.getString("equipe1"));
      this.setEquipe2(jObj.getString("equipe2"));
      this.setScoreEquipe1(jObj.getInt("scoreEquipe1"));
      this.setScoreEquipe2(jObj.getInt("scoreEquipe2"));

      Competition c = new Competition();
      c.fromJSON(jObj.getString("competition"));
      this.setCompetition(c);

      Departement d = new Departement();
      d.fromJSON(jObj.getString("departement"));
      this.setDepartement(d);

      this.setLatitude(jObj.getString("latitude"));
      this.setLongitude(jObj.getString("longitude"));
      this.setShortDescription(jObj.getString("shortDescription"));
      this.setLongDescription(jObj.getString("longDescription"));
      this.setDebut(jObj.getString("debut"));

      Sport s = new Sport();
      s.fromJSON(jObj.getString("sport"));
      this.setSport(s);

      jArr = jObj.getJSONArray("evenements");
      for ( int i=0; i<jArr.length(); i++)
      {
        JSONObject jObjEvenements = jArr.getJSONObject(i);
        Evenement e = new Evenement();

        e.setId(jObjEvenements.getInt("id"));
        e.setCommentaire(jObjEvenements.getString("commentaire"));

        this.arrliEvenements.add(e);
      }
      
    }
    catch (JSONException e)
    {
      e.printStackTrace();
    }
  }
   
}
